<?php 
/**
 * Gerber official online store list:
 * https://www.gerber.com/store-locator#online
 */

namespace app\bee\controller;
use think\Controller;
use think\Cache;
use \DOMDocument;
use \DateTime;

class Gerber extends BeeController {
  public $stores = [
    [
      "name" => "Amazon",
      "url" => "https://www.amazon.com/l/11875032011"
    ],
    [
      "name" => "Dipers",
      "url" => "https://www.amazon.com/s/ref=sr_nr_p_89_3?fst=as%3Aoff&rh=n%3A165796011%2Cn%3A%21165797011%2Cn%3A166777011%2Cn%3A16323111%2Cp_6%3AATVPDKIKX0DER%7CA910SOE1FKRQR%2Cn%3A16323151%2Cp_89%3AGerber&bbn=16323151&ie=UTF8&qid=1500537282&rnid=2528832011",
      "node" => "Amaozn 旗下"
    ],
    [
      "name" => "Toysrus",
      "url" => "https://www.toysrus.com/subcat?categoryid=59120356",
    ],
    [
      "name" => "Target",
      "url" => "https://www.target.com/c/cereals-gerber-grocery-featured-brands/-/N-4rh0h?lnk=cereals",
    ],
    [
      "name" => "Walmart",
      "code" => "walmart",
      // "url" => "https://www.walmart.com/c/brand/gerber-baby-food?cat_id=5427_133283_1001448&facet=type%3ACereals+%26+Grains%7C%7Cbrand%3AGerber&ps=40&sort=best_seller"
      "url" => "https://www.walmart.com/c/brand/gerber-baby-food?cat_id=5427_133283_1001448&facet=type%3ACereals+%26+Grains%7C%7Cbrand%3AGerber&ps=40&sort=best_seller"
    ],
    [
      "name" => "Target",
      "url" => "https://www.target.com/c/cereals-gerber-grocery-featured-brands/-/N-4rh0h?lnk=cereals"
    ],
  ];
  public function index() {
    $vendor = 'app\bee\controller\Target';
    $url = "https://www.target.com/c/cereals-gerber-grocery-featured-brands/-/N-4rh0h?lnk=cereals";
    $jsonURL = "https://redsky.target.com/v1/plp/search?count=24&offset=0&category=4rh0h&visitorId=015C9ADDFB810101544ED8ECA771D676&channel=web&pageId=/c/";
    $vendorClass = new $vendor;
    $products = $vendorClass->getProducts($jsonURL, 'json');
    $this->assign('products', $products);
    return view();
  }
  public function walmart() {
    $store_name = 'Walmart';
    $store_class = "app\\bee\\controller\\".$store_name;
    $store_obj = new $store_class;
    $store_data = array(); 
    
    // ! array_search 不支持数组搜索(?)
    // $search_value = array('code' => $store_code);
    // $store_array_key = array_search($search_value, $this->stores);
    
    foreach ($this->stores as $store) {
      if (isset($store['name']) && $store['name'] == $store_name) {
        $store_data = $store;
      }
    }
    $opt = [
      'categoryId' => '5427_133283_1001448',
      // 'facet.filter' => 'brand:Gerber',
      'facet.filter' => 'type:Cereals & Grains'
    ];
    $products_info = $store_obj->getProductsByAPI('gerber', $opt);
    // var_dump($products_info);
    $this->assign('products_info', $products_info);
    return view();

    // $vendors = $this->vendors;
    
    
    // $url = 
  }
  public function getVendors() {
    $stores = [
      [
        "name" => "Amazon",
        "url" => "https://www.amazon.com/l/11875032011"
      ],
      [
        "name" => "Dipers",
        "url" => "https://www.amazon.com/s/ref=sr_nr_p_89_3?fst=as%3Aoff&rh=n%3A165796011%2Cn%3A%21165797011%2Cn%3A166777011%2Cn%3A16323111%2Cp_6%3AATVPDKIKX0DER%7CA910SOE1FKRQR%2Cn%3A16323151%2Cp_89%3AGerber&bbn=16323151&ie=UTF8&qid=1500537282&rnid=2528832011",
        "node" => "Amaozn 旗下"
      ],
      [
        "name" => "Toysrus",
        "url" => "https://www.toysrus.com/subcat?categoryid=59120356",
      ],
      [
        "name" => "Target",
        "url" => "https://www.target.com/c/cereals-gerber-grocery-featured-brands/-/N-4rh0h?lnk=cereals",
      ],
      [
        "name" => "Walmart",
        "code" => "walmart",
        "url" => "https://www.walmart.com/c/brand/gerber-baby-food?cat_id=5427_133283_1001448&facet=type%3ACereals+%26+Grains%7C%7Cbrand%3AGerber&ps=40&sort=best_seller"
      ],
      [
        "name" => "Target",
        "url" => "https://www.target.com/c/cereals-gerber-grocery-featured-brands/-/N-4rh0h?lnk=cereals"
      ],
      
    ];
  }
  public function getProductsInfoFromAmazon() {
    
  }
}
